package com.sskj.login.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.LazyLoaders;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.ValueScheme;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.InputUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.login.R;
import com.sskj.login.R2;
import com.sskj.login.presenter.PwdForgetActivityPresenter;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.LOGIN_PWD_FORGET)
public class PwdForgetActivity extends BaseActivity<PwdForgetActivityPresenter> {

    @BindView(R2.id.etAccount)
    EditText etAccount;

    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.tvGetCheckCode)
    TextView tvGetCheckCode;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R2.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @BindView(R2.id.tvPwdFind)
    TextView tvPwdFind;
    @BindView(R2.id.tvEmailFind)
    TextView tvEmailFind;
    @BindView(R2.id.llcode)
    LinearLayout llcode;
    @BindView(R2.id.tvCode)
    TextView tvCode;
    private boolean isMobile = true;
    private Disposable timeDis;
    private AndroidNextInputs inputs;
    private AndroidNextInputs inputsCheckCode;
    private boolean isSee;
    private boolean isSeeAgain;
    private int type = 1;
    private BottomSheetDialog priceModeSheet;

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity_pwd_forget;
    }

    @Override
    public PwdForgetActivityPresenter getPresenter() {
        return new PwdForgetActivityPresenter();
    }

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected void initView() {

        ClickUtil.click(200, ivPwdToggleAgain, () -> {
            isSeeAgain = !isSeeAgain;

            etPwdAgain.setTransformationMethod(isSeeAgain ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggleAgain.setImageResource(isSeeAgain ? R.mipmap.login_icon_show : R.mipmap.login_icon_hide);
        });
        ClickUtil.click(200, ivPwdToggle, () -> {
            isSee = !isSee;
            etPwd.setTransformationMethod(isSee ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
            ivPwdToggle.setImageResource(isSee ? R.mipmap.login_icon_show : R.mipmap.login_icon_hide);

        });
        LazyLoaders lazyLoaders = new LazyLoaders(this);

        inputs = new AndroidNextInputs();
        inputs.clear();
        inputs
                .add(etAccount, StaticScheme.NotBlank(), SchemeUtil.getEmailPhoneScheme())
                .add(etCode, StaticScheme.NotBlank())
                .add(etPwd, StaticScheme.NotBlank(), SchemeUtil.getPwdScheme())
                .add(etPwdAgain, StaticScheme.NotBlank(), ValueScheme.equalsTo(lazyLoaders.fromEditText(etPwd)))
        ;
        inputsCheckCode = new AndroidNextInputs();
        inputsCheckCode.clear();
        inputsCheckCode
                .add(etAccount, StaticScheme.NotBlank(), SchemeUtil.getEmailPhoneScheme());

        ClickUtil.click(tvPwdFind, () -> {
            tvPwdFind.setTextColor(getResources().getColor(R.color.commonWhite));
            tvPwdFind.setTextSize(25);
            tvEmailFind.setTextColor(getResources().getColor(R.color.login_b2b));
            tvEmailFind.setTextSize(18);
            etAccount.setHint(App.INSTANCE.getString(R.string.login_loginActivity4_j));
            llcode.setVisibility(View.VISIBLE);
            etAccount.setPadding(140, 0, 0, 0);
            type = 1;
        });

        ClickUtil.click(tvEmailFind, () -> {
            tvEmailFind.setTextColor(getResources().getColor(R.color.commonWhite));
            tvEmailFind.setTextSize(25);
            tvPwdFind.setTextColor(getResources().getColor(R.color.login_b2b));
            tvPwdFind.setTextSize(18);
            etAccount.setHint(App.INSTANCE.getString(R.string.login_loginActivity5_j));
            llcode.setVisibility(View.GONE);
            etAccount.setPadding(0, 0, 0, 0);
            type = 2;
        });

        ClickUtil.click(llcode, () -> {
            if (priceModeSheet == null) {
                priceModeSheet = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, v) -> {
                    priceModeSheet.dismiss();
                    position = position + 1;
                    if (position == 1) {
                        tvCode.setText("60");
                    } else if (position == 2) {
                        tvCode.setText("852");
                    } else if (position == 3) {
                        tvCode.setText("853");
                    } else if (position == 4) {
                        tvCode.setText("65");
                    } else if (position == 5) {
                        tvCode.setText("63");
                    } else if (position == 6) {
                        tvCode.setText("82");
                    } else if (position == 7) {
                        tvCode.setText("81");
                    } else if (position == 8) {
                        tvCode.setText("001");
                    } else if (position == 9) {
                        tvCode.setText("66");
                    } else if (position == 10) {
                        tvCode.setText("886");
                    }
                }, "60", "852", "853", "65", "63", "82", "81", "001", "66", "886");
            }
            priceModeSheet.show();
        });

        ClickUtil.click(tvGetCheckCode, () -> {
            if (TextUtils.isEmpty(etAccount.getText().toString())) {
                if (type == 1) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity1_j));
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity2_j));
                }
                return;
            }
            if (type == 1) {
                if (tvCode.getText().toString().equals("86")) {
                    if (inputsCheckCode.test()) {
                        if (etAccount.getText().toString().trim().length() != 11) {
                            ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                            return;
                        }
                    }
                }
                if (tvCode.getText().toString().equals("60")) {
                    if (etAccount.getText().toString().trim().length() != 9 && etAccount.getText().toString().trim().length() != 10) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("852")) {
                    if (etAccount.getText().toString().trim().length() != 8) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("853")) {
                    if (etAccount.getText().toString().trim().length() != 8) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("65")) {
                    if (etAccount.getText().toString().trim().length() != 8) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("63")) {
                    if (etAccount.getText().toString().trim().length() != 10) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("82")) {
                    if (etAccount.getText().toString().trim().length() != 10 && etAccount.getText().toString().trim().length() != 11) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("81")) {
                    if (etAccount.getText().toString().trim().length() != 10 && etAccount.getText().toString().trim().length() != 11) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("001")) {
                    if (etAccount.getText().toString().trim().length() != 10) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("66")) {
                    if (etAccount.getText().toString().trim().length() != 9 && etAccount.getText().toString().trim().length() != 10) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
                if (tvCode.getText().toString().equals("886")) {
                    if (etAccount.getText().toString().trim().length() != 9) {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.login_loginActivity3_j));
                        return;
                    }
                }
            }
            tvGetCheckCode.setEnabled(false);
            mPresenter.sendCode(tvCode.getText().toString(), etAccount.getText().toString());
        });
        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                mPresenter.resetLoginPwd(
                        tvCode.getText().toString(),
                        etAccount.getText().toString(),
                        etPwd.getText().toString(),
                        etPwdAgain.getText().toString(),
                        etCode.getText().toString()
                );
            }
        });
    }

    public void sendCheckCodeSuccess() {
        DisposUtil.close(timeDis);
        timeDis = TimeUtil.newTime(this)
                .subscribe(aLong -> {
                    if (aLong > 0) {
                        tvGetCheckCode.setText(aLong + "s");
                    } else {
                        tvGetCheckCode.setText(App.INSTANCE.getString(R.string.login_registerActivity7_j));
                        tvGetCheckCode.setEnabled(true);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void sendCheckCodeFail() {
        tvGetCheckCode.setEnabled(true);
    }

    public void resetPwdSuccess() {
        finish();
    }
}
